<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Request;
use \think\facade\Session;
use think\facade\Config;
use \think\Db;


/*询问数据*/
class Searchc extends Base{

	/*列表*/
	public function index(Request $request){
		$this->check_session();
        /*左侧导航*/
        $data_get=$request->get()['nav'];

        $data=$this->navleft($data_get);

        $this->assign($data);

	    return $this->fetch();
	}


	public function get_result_c(Request $request){
		$this->check_session();
		$get=$request->get();
		if(empty($get['start'])&&empty($get['end'])){
			$get['start']=date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
			$get['end']=date('Y-m-d 23:59:59', time());
		}


		$model=new \app\admin\model\Searchcount();/*调用模型查询*/
		$model_data=$model->get_country($get['page'],$get['limit'],$get['start'],$get['end'],$get['fields']);
		
		foreach ($model_data['list'] as $key => $value) {
		    $str='';
		    foreach ($value['keywords'] as $k => $v) {
		   	    $str.=$v[$get['fields']].'('.$v['b'].'), ';
		    }
        	$model_data['list'][$key]['str']=rtrim($str,', ');
        }

        $result=["code"=>0,'msg'=>"","count"=>$model_data['count'],"data"=>$model_data['list']];
        return json($result);
	}

	public function get_result_xsl(Request $request){
		$get=$request->get();

	    $data['fields']=$get['fields'];
	    if($get['fields']=='sou'){
	    	$str='回车';
	    	$titn='搜索内容';
	    	$titc='搜索次数';
	    }
	    if($get['fields']=='noresult'){
	    	$str='无结果';
	    	$titn='搜索内容';
	    	$titc='搜索次数';
	    }
	    if($get['fields']=='leftclick'){
	    	$str='左侧点击';
	    	$titn='点击内容';
	    	$titc='点击次数';
	    }
	    if($get['fields']=='rightclick'){
	    	$str='右侧点击';
	    	$titn='点击内容';
	    	$titc='点击次数';
	    }
	    $data['title']=$get['country'].$str.'统计数据';
	    $data['shijian']='统计时间: '.$get['starts_time'].' - '.$get['end_time'];
	    $data['get']=$get;
	    $data['titn']=$titn;
	    $data['titc']=$titc;

	    $this->assign($data);

	    return $this->fetch();
	}

	public function get_result_xsl_deal(Request $request){
		$get=$request->get();
		$model=new \app\admin\model\Searchcount();/*调用模型查询*/
		$model_data=$model->get_country_xsl($get['country'],$get['starts_time'],$get['end_time'],$get['fields']);

	    foreach ($model_data as $key => $value) {
	    	$str='';
	        foreach ($value['detail'] as $k => $v) {
	        	$Mobile=$this->isMobile($v['sys']);
	        	if($Mobile){
					$isMobile='phone';
				}else{
					$isMobile='pc';
				}
				$str.='{ '.$v['ip'].' | '.$isMobile.' | '.$v['create_time'].' } , ';
	    	}
	    	$model_data[$key]['str']=rtrim($str,', ');
	    }

	    $result=["code"=>0,'msg'=>"","count"=>$model_data,"data"=>$model_data];
        return json($result);
	}

	public function isMobile($anget) {
		static $is_mobile = null; 
		if ( isset( $is_mobile ) ) {
			return $is_mobile;
		} 
		if ( empty($anget) ) {
				$is_mobile = false;
		} elseif ( strpos($anget, 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
				|| strpos($anget, 'Android') !== false
				|| strpos($anget, 'Silk/') !== false
				|| strpos($anget, 'Kindle') !== false
				|| strpos($anget, 'BlackBerry') !== false
				|| strpos($anget, 'Opera Mini') !== false
				|| strpos($anget, 'Opera Mobi') !== false ) {
				$is_mobile = true;
		} else {
			$is_mobile = false;
		} 
		return $is_mobile;
    }
}